package com.nmsl.statistic.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.nmsl.statistic.constants.Constants;
import com.nmsl.statistic.domain.OperInfo;
import com.nmsl.statistic.domain.RetailMedicine;
import com.nmsl.statistic.mapper.DailyFlowMapper;
import com.nmsl.statistic.mapper.OperInfoMapper;
import com.nmsl.statistic.mapper.RetailMedicineMapper;
import com.nmsl.statistic.utils.DailyFlowUtils;
import com.nmsl.statistic.utils.RedisUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nmsl.common.annotation.Log;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.enums.BusinessType;
import com.nmsl.statistic.domain.TollManage;
import com.nmsl.statistic.service.ITollManageService;
import com.nmsl.common.utils.poi.ExcelUtil;
import com.nmsl.common.core.page.TableDataInfo;

/**
 * 收费管理Controller
 * 
 * @author mcslf
 * @date 2025-02-15
 */
@RestController
@RequestMapping("/statistic/statistic")
public class TollManageController extends BaseController
{
    @Autowired
    private ITollManageService tollManageService;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RetailMedicineMapper retailMedicineMapper;

    @Autowired
    private DailyFlowMapper dailyFlowMapper;

    /**
     * 查询收费管理列表
     */
//    @PreAuthorize("@ss.hasPermi('statistic:statistic:list')")
    @GetMapping("/list")
    public TableDataInfo list(TollManage tollManage)
    {
//        queryParams: {
//            pageNum: 1,
//                    pageSize: 10,
//                    patientId: 1
//        }
        startPage();
        List<TollManage> list = tollManageService.selectTollManageList(tollManage);
        System.out.println("查询一下打印出的数据");
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出收费管理列表
     */
    @PreAuthorize("@ss.hasPermi('statistic:statistic:export')")
    @Log(title = "收费管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TollManage tollManage)
    {
        List<TollManage> list = tollManageService.selectTollManageList(tollManage);
        ExcelUtil<TollManage> util = new ExcelUtil<TollManage>(TollManage.class);
        util.exportExcel(response, list, "收费管理数据");
    }

    /**
     * 获取收费管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('statistic:statistic:query')")
    @GetMapping(value = "/{tollManageId}")
    public AjaxResult getInfo(@PathVariable("tollManageId") Long tollManageId)
    {
        return success(tollManageService.selectTollManageByTollManageId(tollManageId));
    }

    /**
     * 新增收费管理
     */
    @PreAuthorize("@ss.hasPermi('statistic:statistic:add')")
    @Log(title = "收费管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TollManage tollManage)
    {
        return toAjax(tollManageService.insertTollManage(tollManage));
    }

    /**
     * 修改收费管理
     */
    @PreAuthorize("@ss.hasPermi('statistic:statistic:edit')")
    @Log(title = "收费管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TollManage tollManage)
    {
        return toAjax(tollManageService.updateTollManage(tollManage));
    }

    /**
     * 删除收费管理
     */
    @PreAuthorize("@ss.hasPermi('statistic:statistic:remove')")
    @Log(title = "收费管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tollManageIds}")
    public AjaxResult remove(@PathVariable Long[] tollManageIds)
    {
        return toAjax(tollManageService.deleteTollManageByTollManageIds(tollManageIds));
    }

    @RequestMapping("/retialAdd")
    public AjaxResult retialAdd(@RequestBody HashMap<String,Object> map){
        tollManageService.retialAdd(map);
        return  AjaxResult.success();
    }

    @Autowired
    OperInfoMapper operInfoMapper;
    @RequestMapping("/oper")
    public AjaxResult getOper(){
        List<OperInfo> list = operInfoMapper.selectAll();
        System.out.println(list);
        return AjaxResult.success(list);
    }


    @RequestMapping("/card/pay/{totail}")
    public AjaxResult cardpay(@PathVariable String totail){
        System.out.println(totail);
        TollManage tollManage = new TollManage();
        tollManage.setTollManageId(Long.valueOf(totail));
        tollManage.setStatus("1");
        tollManage.setPayType("会员卡");
//      增加药品销量
        //        药品销量增加
        String orderId = tollManage.getOrderId();
        String orderType = tollManage.getOrderType();
        String key = orderType.equals("1") ? Constants.medicinesRetailRanking : Constants.medicinesRecipeRanking;
//        查询绑定药品信息  定时任务持久化
        List<RetailMedicine> list = retailMedicineMapper.selecetByRetailOrderId(Long.valueOf(orderId));

        for (RetailMedicine item : list) {
            RedisUtil.updataMedicinesRanking(redisTemplate,key,item.getMedicineId(),item.getMedicineNum().intValue());
            RedisUtil.updataMedicinesRanking(redisTemplate,Constants.medicinesRanking,item.getMedicineId(),item.getMedicineNum().intValue());

        }
//        计算每日流水
        DailyFlowUtils.payCart(dailyFlowMapper,tollManage,redisTemplate);
        tollManageService.updateTollManage(tollManage);
        return AjaxResult.success();
    }
}
